<?php

require_once 'FWidget.class.php';

class FButton extends FWidget
{
    
    private $function;
    private $icon;
    private $iconAlign;
    
   const ICON_ALIGN_LEFT = 'left';
   const ICON_ALIGN_RIGHT = 'right';
   const ICON_ALIGN_TOP = 'top';
   const ICON_ALIGN_BOTTOM = 'bottom';
    
    public function __construct($name, $text, $function, $width = 100)
    {        
        parent::__construct($name, 'fbutton');
        $this->setText($text);
        $this->setFunction($function);
        $this->setWidth($width);
        $this->setHandler('_submit');
    }
    
    public function generate()
    {
        $jsCode = "
            {
                xtype: '{$this->getXtype()}',
                name: '{$this->getName()}',
                text: '{$this->getText()}',
                scope: this,
                handler: this.{$this->getHandler()},
                width: {$this->getWidth()},
                function: '{$this->getFunction()}',
                icon: '{$this->getIcon()}',
                iconAlign: '{$this->getIconAlign()}',
                iconCls: 'button-icon'
            }
        ";
                
        return $jsCode;
    }
    
    public function getFunction()
    {
        if ( !$this->function )
        {
            $this->function = $this->getName() . '_click';
        }
        
        return $this->function;
    }

    public function setFunction($function) {
        $this->function = $function;
    }
    
    public function getIcon() {
        return $this->icon;
    }

    public function setIcon($icon) {
        $this->icon = $icon;
    }
    
    public function getIconAlign() {
        return $this->iconAlign ? $this->iconAlign : FButton::ICON_ALIGN_TOP;
    }

    public function setIconAlign($iconAlign) {
        $this->iconAlign = $iconAlign;
    }
    
}

?>
